﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace telWebBrowserNet.Lib
{
    public class LogInfo
    {
        public static string log_info = "INFO";
        public static string log_warn = "WARN";
        public static string log_error = "ERROR";
        public static string log_debug = "DEBUG";
        public static string log_trace = "TRACE";

        private static Form1 form1 = null;

        public static void setForm1(Form1 form)
        {
            form1 = form;
        }

        public static void LogOutput(string type, string log)
        {

            //string logPath = Path.GetDirectoryName(Application.ExecutablePath) + "\\log";
            string sFilePath = Path.GetDirectoryName(Application.ExecutablePath) + "\\log" + "\\" + DateTime.Now.ToString("yyyyMM");
            string sFileName = DateTime.Now.ToString("dd") + ".log";
            sFileName = sFilePath + "\\" + sFileName; //文件的绝对路径
            if (!Directory.Exists(sFilePath))//验证路径是否存在
            {
                Directory.CreateDirectory(sFilePath); //不存在则创建
            }
            FileStream fs;
            StreamWriter sw;
            if (File.Exists(sFileName)) //文件是否存在，有则追加，无则创建
            {
                fs = new FileStream(sFileName, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
            }
            else
            {
                fs = new FileStream(sFileName, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
            }
            sw = new StreamWriter(fs);
            string logTxt = DateTime.Now.ToString("[HH:mm:ss]") + "[" + type + "]" + log;
            sw.WriteLine(logTxt);//yyyy-MM-dd 
            sw.Close();
            fs.Close();
            if(form1 != null)
            {
                form1.SetLog(logTxt+"\r\n");
            }
        }
    }
}
